﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SunXNA.CameraManager;
using System.Diagnostics;

namespace SunXNA.RainbowXNA
{
    class CViewport
    {
        #region Public Types
        #endregion

        #region Private Member Variables

        private Viewport m_XNAViewPort;
        private float m_rAspectRatio;
        private CCameraManager.CameraHandle m_cameraHandle;
        private Color m_clearColour;
        private BoundingFrustum m_frustrum;

        #endregion

        #region Public Accessors

        public Color ClearColour
        {
            get
            {
                return m_clearColour;
            }

            set
            {
                m_clearColour = value;
            }
        }

        public float AspectRatio
        {
            get
            {
                return m_rAspectRatio;
            }
        }

        public int X
        {
            get
            {
                return m_XNAViewPort.X;
            }

            set
            {
                m_XNAViewPort.X = value;
            }
        }

        public int Y
        {
            get
            {
                return m_XNAViewPort.Y;
            }

            set
            {
                m_XNAViewPort.X = value;
            }
        }

        public int Width
        {
            get
            {
                return m_XNAViewPort.Width;
            }

            set
            {
                m_XNAViewPort.Width = value;
                m_rAspectRatio = (float)m_XNAViewPort.Width / (float)m_XNAViewPort.Height;
            }
        }

        public int Height
        {
            get
            {
                return m_XNAViewPort.Height;
            }

            set
            {
                m_XNAViewPort.Height = value;
                m_rAspectRatio = (float)m_XNAViewPort.Width / (float)m_XNAViewPort.Height;
            }
        }

        public Viewport XNAViewport
        {
            get
            {
                return m_XNAViewPort;
            }
        }

        public CCameraManager.CameraHandle CameraHandle
        {
            get
            {
                return m_cameraHandle;
            }

            set
            {
                m_cameraHandle = value;
            }
        }

        public BoundingFrustum Frustrum
        {
            get
            {
                return m_frustrum;
            }
        }

        #endregion

        #region Public Member Functions

        public CViewport( Rectangle viewportArea, CCameraManager.CameraHandle cameraHandle, Color clearColour )
        {
            m_XNAViewPort.X = viewportArea.X;
            m_XNAViewPort.Y = viewportArea.Y;
            m_XNAViewPort.Width = viewportArea.Width;
            m_XNAViewPort.Height = viewportArea.Height;

            m_rAspectRatio = (float)m_XNAViewPort.Width / (float)m_XNAViewPort.Height;

            m_cameraHandle = cameraHandle;

            m_clearColour = clearColour;

            m_frustrum = new BoundingFrustum(Matrix.Identity);

            Matrix m4View;
            Matrix m4Projection;
            CalculateMatrices(out m4Projection, out m4View);
        }

        public void CalculateMatrices( out Matrix outProjectionMatrix, out Matrix outViewMatrix )
        {
            CameraManager.BaseCamera.CBaseCamera camera = m_cameraHandle.Camera;
            Trace.Assert(camera != null);

            float rFov = camera.FOV;//MathHelper.Lerp(camera.PreviousFOV, camera.FOV, rStepFraction);
            outProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(rFov, m_rAspectRatio, 1.0f, 500.0f);

            outViewMatrix = camera.CurrentViewMatrix; //Matrix.Lerp(camera.PreviousViewMatrix, camera.CurrentViewMatrix, rStepFraction);

            m_frustrum.Matrix = outViewMatrix * outProjectionMatrix;
        }

        #endregion
    }
}
